export default {
  // 选择了数据
  handleSelectionChange(val) {
    this.$emit("update:multipleSelection", val);
  },
  // 点击查询按钮
  handlerSearch() {
    this.page.currentPage = 1;
    this.interfacePage[this.pageConfig.currentPage] = 1;
    this.search();
  },
  // 点击弹出框或抽屉保存的按钮
  handleDialogSubmit() {
    this.$emit('dialogSubmit', this.drawerConfig)
  },
  // 搜索
  search() {
    this.resultSubmitForm();
    if (this.getList && typeof this.getList === "function") {
      this.btnLoading = true;
      this.tableLoading = true;
      const obj = Object.assign({}, this.interfacePage, this.formInline, this.newParams);
      this.getList(obj)
        .then((res) => {
          this.$nextTick(() => {
            this.$refs.multipleTable.doLayout()
          })
          if (Array.isArray(res)) {
            this.tableData = res;
          } else {
            let total = res.totalElements;
            this.page.total = total;
            this.tableData = res.list;
            this.json_data = res.list;
          }
          this.btnLoading = false;
          this.tableLoading = false;
        })
        .catch(() => {
          this.btnLoading = false;
          this.tableLoading = false;
        });
    } else {
      this.tableData = [];
    }
  },
  // 整合formInline
  resultSubmitForm() {
    if (Array.isArray(this.forms)) {
      this.forms.forEach((ele) => {
        if (ele.type === 'cascader') {
          if (Array.isArray(ele.model) && ele.model.length > 0) {
            if (ele.state === "multiple") {
              const str = ele.model.toString();
              this.formInline[ele.prop] = str;
            } else {
              this.formInline[ele.prop] = ele.model[ele.model.length - 1];
            }
          } else {
            this.formInline[ele.prop] = "";
          }
        } else {
          if (Array.isArray(ele.model) && ele.model.length > 0) {
            this.formInline[ele.prop[0]] = ele.model[0];
            this.formInline[ele.prop[1]] = ele.model[1];
          } else if (Array.isArray(ele.model) && ele.model.length === 0) {
            this.formInline[ele.prop[0]] = "";
            this.formInline[ele.prop[1]] = "";
          } else if (Array.isArray(ele.prop) && ele.model === null) {
            this.formInline[ele.prop[0]] = "";
            this.formInline[ele.prop[1]] = "";
          } else if (ele.type === "intervalAmount" || ele.type === "numberIntervals") {
            this.formInline[ele.prop[0]] = ele.minVal;
            this.formInline[ele.prop[1]] = ele.maxVal;
          } else {
            this.formInline[ele.prop] = ele.model;
          }
        }
      });
    }
  },
  // 上传文件对文件类型的判断
  beforeUpload(file) {
    var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
    const extension = testmsg === 'xls'
    const extension2 = testmsg === 'xlsx'
    // const isLt2M = file.size / 1024 / 1024 < 10
    if (!extension && !extension2) {
      this.$message({
        message: '上传文件只能是 xls、xlsx格式!',
        type: 'warning'
      });
    }
    // if(!isLt2M) {
    //     this.$message({
    //         message: '上传文件大小不能超过 10MB!',
    //         type: 'warning'
    //     });
    // }
    // return (extension || extension2) && isLt2M
    return extension || extension2
  },
  // 上传成功的效果
  handleSuccess(file) {
    console.log(file)
    // let obj = {
    //   fileName: "",
    //   fileUrl: "",
    //   uuid: ""
    // }
    // obj = { ...file }
  },
  // 下载文件
  async fetchData() {
    if (this.multipleSelection.length == 0) {
      return
    }
    let list = [];
    let page = 1;
    if (!this.getList) {
      list = [];
    } else {
      const x = Math.ceil(this.page.total / this.downloadSize);
      if (x <= 1) {
        list = await this.tableMore(page);
      } else {
        const arr = []
        await this.getExcelData(page, arr, x)
        list = arr;
      }
    }
    return list;
  },
  // 当表格数据超过规定的数据时，采用递归函数
  async getExcelData(page, arr, x) {
    const res = await this.tableMore(page);
    if (Array.isArray(res)) {
      res.forEach(ele => {
        arr.push(ele)
      })
    }
    if (page < x) {
      page++;
      await this.getExcelData(page, arr, x)
    }
  },
  // 下载时表格从后台获取数据
  async tableMore(page) {
    let list = []
    const pageObj = Object.assign({}, this.interfacePage)
    pageObj[this.pageConfig.pageSize] = this.downloadSize;
    pageObj[this.pageConfig.currentPage] = page;
    const obj = Object.assign({}, pageObj, this.formInline);
    const res = await this.getList(obj);
    if (Array.isArray(res)) {
      list = res;
    } else {
      list = res.list;
    }
    // 对数据的处理保证都是中文输出
    this.tableConfig.forEach(ele => {
      if (ele.format) {
        list.forEach(e => {
          e[ele.prop] = ele.format(e[ele.prop]);
        })
      }
    })
    return list
  },
  // 清空
  clearForm() {
    if (Array.isArray(this.forms)) {
      this.forms.forEach((ele) => {
        if (
          ele.type === "input" ||
          ele.type === "select" ||
          ele.type === "radio"
        ) {
          ele.model = "";
        } else if (ele.type === "datePicker") {
          ele.model = [];
        } else if (
          ele.type === "timePicker" ||
          ele.type === "datetimerange"
        ) {
          ele.model = null;
        } else if (ele.type === "cascader") {
          ele.model = [];
        } else if (ele.type === "checkbox") {
          ele.model = [];
          if (ele.isAll) {
            ele.checkAll = false;
            ele.isIndeterminate = false;
          }
        } else if (ele.type === "intervalAmount" || ele.type === "numberIntervals") {
          ele.minVal = ""
          ele.maxVal = ""
        } else if (ele.type === "slot") {
          if (Array.isArray(ele.model)) {
            ele.model[0] = "";
            ele.model[1] = "";
          } else {
            ele.model = "";
          }
        }
      });
    }
  },
  //打开抽屉
  openDrawer(str, row) {
    this.drawerConfig.drawerVisible = true;
    if (row && row.constructor === Object) {
      this.drawerConfig.drawerRow = Object.assign({}, row);
    }
    if (str === "details") {
      this.drawerConfig.drawerTitle = "查看详情";
      this.drawerConfig.drawerSlotName = "details";
      this.drawerConfig.drawerType = "details";
      this.$emit("details", this.drawerConfig);
      if (this.detailsDialog) {
        this.drawerConfig.drawerShow = true;
      }
    } else if (str === "edit") {
      this.drawerConfig.drawerTitle = "编辑";
      this.drawerConfig.drawerSlotName = "edit";
      this.drawerConfig.drawerType = "edit";
      this.$emit("edit", this.drawerConfig);
      if (this.editDialog) {
        this.drawerConfig.drawerShow = true;
      }
    } else if (str === "add") {
      this.drawerConfig.drawerTitle = "新增";
      this.drawerConfig.drawerRow = "";
      this.drawerConfig.drawerSlotName = "add";
      this.drawerConfig.drawerType = "add";
      this.$emit("add", this.drawerConfig);
      if (this.addDialog) {
        this.drawerConfig.drawerShow = true;
      }
    } else if (str === "operateLog") {
      this.drawerConfig.drawerTitle = "操作记录";
      this.drawerConfig.drawerSlotName = "operateLog";
      this.drawerConfig.drawerType = "operateLog";
      this.$emit("operateLog", this.drawerConfig);
      if (this.operateLogDialog) {
        this.drawerConfig.drawerShow = true;
      }
    }
  },
  // 关闭抽屉
  drawClose() {
    this.drawerConfig.drawerVisible = false;
  },
  // 改变每页多少条
  handleSizeChange(val) {
    this.interfacePage[this.pageConfig.pageSize] = val;
    this.search();
  },
  // 改变第几页
  handleCurrentChange(val) {
    this.interfacePage[this.pageConfig.currentPage] = val;
    this.search();
  },
  // 删除的功能
  del(row, index) {
    this.$confirm("确定要删除?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      beforeClose: (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true;
          instance.confirmButtonText = '执行中...';
          if (typeof this.delUrl === "function") {
            this.delUrl(row.uuid).then(() => {
              instance.confirmButtonLoading = false;
              this.tableData.splice(index, 1);
              done();
            }).catch(() => {
              instance.confirmButtonLoading = false;
              instance.confirmButtonText = "确定";
            })
          }

        } else {
          done();
        }
      }
    })
      .then(() => {
        this.$message({
          message: "删除成功",
          type: 'success'
        });

      })
      .catch(() => {
        // 什么也不做
      });
  },

  // 处理checkbox
  changeCheckbox(val, ele) {
    if (ele.isAll) {
      let checkedCount = val.length;
      ele.checkAll = checkedCount === ele.data.length;
      ele.isIndeterminate =
        checkedCount > 0 && checkedCount < ele.data.length;
    }
    this.formInline[ele.prop] = val.toString();
  },
  // 处理checkbox全选
  handleCheckAllChange(val, ele) {
    const checkArr = [];
    ele.data.forEach((e) => {
      checkArr.push(e.value);
    });
    ele.value = val ? checkArr : [];
    ele.isIndeterminate = false;
    this.formInline[ele.prop] = ele.value.toString();
  },
  // 处理select
  doSelect(ele, index) {
    let i = index;
    // 判断是否需要接口返回，有接口的优先
    if (ele.dataUrl) {
      ele.dataUrl(ele.queryData ? ele.queryData : '').then((res) => {
        if (Array.isArray(res)) {
          const arr = [];
          res.forEach((e) => {
            let obj = {};
            if (ele.dataConfig) {
              obj = {
                value: e[ele.dataConfig.value],
                label: e[ele.dataConfig.label],
              };
            } else {
              obj = {
                value: e.value,
                label: e.label || ele.label + i,
              };
            }
            arr.push(obj);
            i++;
          });
          ele.data = arr;
          this.$forceUpdate();
        }
      });
      // 不采用接口获取数据的情况
    } else {
      if (ele.data && Array.isArray(ele.data)) {
        const arr = [];
        ele.data.forEach((e) => {
          let obj = {};
          if (ele.dataConfig) {
            obj = {
              value: e[ele.dataConfig.value],
              label: e[ele.dataConfig.label],
            };
          } else {
            obj = {
              value: e.value,
              label: e.label || ele.label + i,
            };
          }
          arr.push(obj);
          i++;
        });
        ele.data = arr;
      }
    }
  },
  handleClick() {
    this.$emit('handleClick', {})
  },
  getRowKey(row) {
    return row.uuid;
  }
}
